import { _decorator,Component ,Node, Sprite, SpriteFrame } from 'cc';
import { Core } from '../Core/Core';
import { EEvent, EMartialSystem, EView } from '../Config/Enum';
import { ResourceManager } from '../Core/ResourceManager';
import { PicPath } from '../Config/ResourceConfig';


const { ccclass, property } = _decorator;

@ccclass('MartialView')
export class MartialView extends Component {


    ndBG: Node = null!; // 背景节点
    ndChooseBoard: Node = null!; // 选择框节点
    ndFarming: Node = null!; // 农政节点
    ndFortify: Node = null!; // 筑城节点
    ndBeacon: Node = null!; // 烽火节点
    ndSearch: Node = null!; // 搜索节点






    protected onLoad(): void {

        this.ndBG = this.node.getChildByName("BG");
        this.ndChooseBoard = this.node.getChildByName("ChooseBoard");
        this.ndFarming = this.node.getChildByName("Board").getChildByName("Farming");
        this.ndFortify = this.node.getChildByName("Board").getChildByName("Fortify");
        this.ndBeacon = this.node.getChildByName("Board").getChildByName("Beacon");
        this.ndSearch = this.node.getChildByName("Board").getChildByName("Search");


        
        

        
    }

    protected onEnable(): void {
        this.ndBG.on(Node.EventType.TOUCH_END, this.onButtonCloseThisWindow, this);
        Core.Instance.event.on(EEvent.FarmingUpdate, this.onFarmingUpdate, this);

        Core.Instance.currentView = EView.MartialView;

        console.log("MartialView打开，当前已安排工作的义士：", Core.Instance.assignedPlayers);

        // 当前已安排工作的义士的数据
        this.loadData();

        
    }

    protected onDisable(): void {

        this.ndBG.off(Node.EventType.TOUCH_END, this.onButtonCloseThisWindow, this);
        Core.Instance.event.off(EEvent.FarmingUpdate, this.onFarmingUpdate, this);
        
    }

    protected start(): void {
        
    }

    loadData(){

        Object.keys(Core.Instance.assignedPlayers).forEach(key=>{
            if (Core.Instance.assignedPlayers[key] == null) return;
            switch(key){
                case EMartialSystem.Farming:
                    //this.ndFarming.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/avatar");
                    ResourceManager.Instance.loadRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/spriteFrame")
                    .then((spriteFrame) => {
                        this.ndFarming.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = spriteFrame;
                    })
                    .catch((error) => {
                        console.error("加载义士头像失败:", error);
                    });
                    break;
                case EMartialSystem.Fortify:
                    //this.ndFortify.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/avatar");
                    
                    ResourceManager.Instance.loadRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/spriteFrame")
                    .then((spriteFrame) => {
                        this.ndFortify.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = spriteFrame;
                    })
                    .catch((error) => {
                        console.error("加载义士头像失败:", error);
                    });
                    break;
                case EMartialSystem.Beacon:
                    //this.ndBeacon.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/avatar");
                    ResourceManager.Instance.loadRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/spriteFrame")
                    .then((spriteFrame) => {
                        this.ndBeacon.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = spriteFrame;
                    })
                    .catch((error) => {
                        console.error("加载义士头像失败:", error);
                    });
                    break;
                case EMartialSystem.Search:
                    ResourceManager.Instance.loadRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/spriteFrame")
                    .then((spriteFrame) => {
                        this.ndSearch.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = spriteFrame;
                    })
                    .catch((error) => {
                        console.error("加载义士头像失败:", error);
                    });
                    //this.ndSearch.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(PicPath[Core.Instance.assignedPlayers[key] + "Avatar"]+"/avatar");
                    break;
            }
        });


    }


    onFarmingUpdate(value){
        console.log("MartialView收到农政更新事件，选择了："+value);

        //this.ndFarming.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(PicPath[value + "Avatar"]+"/avatar");
        ResourceManager.Instance.loadRes<SpriteFrame>(PicPath[value + "Avatar"]+"/spriteFrame")
        .then((spriteFrame) => {
            this.ndFarming.getChildByName("ChooseBox").getChildByName("Icon").getComponent(Sprite).spriteFrame = spriteFrame;
        })
        .catch((error) => {
            console.error("加载义士头像失败:", error);
        });
        // 更新已安排工作的义士
        Core.Instance.assignedPlayers[EMartialSystem.Farming] = value;

        // 保存
        Core.Instance.save();


    }


    onButtonCloseThisWindow(){
        console.log("关闭窗口")
        this.node.active = false
    }





}


